package com.inspur.cloud.controller;

import com.inspur.cloud.configuration.ApiConfig;
import com.inspur.cloud.entity.dataspace.ao.common.VerifyCodeAO;
import com.inspur.cloud.entity.dataspace.ao.common.VerifyCodeNeedAO;
import com.inspur.cloud.entity.dataspace.common.OpEntityResult;
import com.inspur.cloud.entity.dataspace.common.OpResult;
import com.inspur.cloud.entity.dataspace.vo.VerifyCodeDTO;
import com.inspur.cloud.service.dataspace.VerifyCodeService;
import com.inspur.cloud.util.ExceptionHelper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.security.NoSuchAlgorithmException;

@RestController
@RequestMapping("/dataspace/api/v1/verifyCode")
@Api(value = "verifyCode", description = "验证码")
public class VerifyCodeController {

    private static Logger log = LoggerFactory.getLogger(VerifyCodeController.class);

    @Autowired
    private ApiConfig apiConfig;

    @Autowired
    private VerifyCodeService verifyCodeService;

    @GetMapping
    @ApiOperation(value = "获取验证码", notes = "对接人：soff")
    public OpEntityResult<VerifyCodeAO> verifyCode(HttpServletRequest request) {
        try {
            OpEntityResult<VerifyCodeAO> result = new OpEntityResult();
            VerifyCodeDTO service = verifyCodeService.getVerifyCode(60, 20);
            String code = service.getCode();
            request.getSession().setAttribute("verifyCode", code);
            VerifyCodeAO verifyCode = new VerifyCodeAO();
            verifyCode.setResult(service.getBase64());
            result.setData(verifyCode);
            result.setCode(1);
            result.setMsg("查询成功");
            return result;
        }catch (NoSuchAlgorithmException e){
            log.error("get verifyCode failed:"+ ExceptionHelper.getExceptionDetail(e));
            OpEntityResult<VerifyCodeAO> result = new OpEntityResult();
            result.setCode(-1);
            result.setMsg("获取失败");
            return result;
        }catch (IOException e) {
            log.error("get verifyCode failed:"+ ExceptionHelper.getExceptionDetail(e));
            OpEntityResult<VerifyCodeAO> result = new OpEntityResult();
            result.setCode(-1);
            result.setMsg("获取失败");
            return result;
        }
    }

    @GetMapping("/need")
    @ApiOperation(value = "是否需要验证码", notes = "对接人：soff")
    public OpResult need(){
        OpEntityResult<VerifyCodeNeedAO> result = new OpEntityResult();
        VerifyCodeNeedAO verifyCodeNeed = new VerifyCodeNeedAO();
        verifyCodeNeed.setNeed(apiConfig.isVerifyCode());
        result.setCode(1);
        result.setMsg("查询成功");
        result.setData(verifyCodeNeed);
        return result;
    }
}
